package com.xyzq.hs.util;

import java.util.Arrays;
import java.util.regex.Pattern;

public class LoggerImpl implements Logger{
	
	private int level=LoggerLevel.DEFAULT;
	
	
	LoggerImpl() {
		super();
	}

	LoggerImpl(int level) {
		super();
		this.level = level;
	}
	
	public void all(String msg) {
		if(isAll()){
			print(msg);
		}
	}

	@Override
	public void all(String format, Object... arguments) {
		if(isAll()){
			printf(format,arguments);
		}
	}

	@Override
	public void debug(String msg) {
		if(isDebug()){
			print(msg);
		}
		
	}

	@Override
	public void debug(String format, Object... arguments) {
		if(isDebug()){
			printf(format,arguments);
		}
	}

	@Override
	public void info(String msg) {
		if(isInfo()){
			print(msg);
		}
	}

	@Override
	public void info(String format, Object... arguments) {
		if(isInfo()){
			printf(format,arguments);
		}
	}
	
	@Override
	public void warn(String msg) {
		if(isWarn()){
			print(msg);
		}
	}
	
	@Override
	public void warn(String format, Object... arguments) {
		if(isWarn()){
			printf(format,arguments);
		}
	}

	@Override
	public void error(String msg) {
		if(isError()){
			print(msg);
		}
	}

	@Override
	public void error(String format, Object... arguments) {
		if(isError()){
			printf(format,arguments);
		}
	}
	

	@Override
	public void fatal(String msg) {
		if(isFatal()){
			print(msg);
		}
	}

	@Override
	public void fatal(String format, Object... arguments) {
		if(isFatal()){
			printf(format,arguments);
		}
		
	}
	
	@Override
	public boolean isAll() {
		return level<=LoggerLevel.ALL;
	}
	
	@Override
	public boolean isDebug() {
		return level<=LoggerLevel.DEBUG;
	}

	@Override
	public boolean isInfo() {
		return level<=LoggerLevel.INFO;
	}

	@Override
	public boolean isWarn() {
		return level<=LoggerLevel.WARN;
	}

	@Override
	public boolean isError() {
		return level<=LoggerLevel.ERROR;
	}

	@Override
	public boolean isFatal() {
		return level<=LoggerLevel.FATAL;
	}
	
	
	private void print(String msg){
		System.out.println(msg);
	}
	
	private void printf(String msg, Object... arguments){
		Pattern pattern = Pattern.compile("^\\{\\}$");
		String[] a = pattern.split(msg);
		System.out.println(Arrays.toString(a));
		if(a.length!=arguments.length){
			throw new IllegalArgumentException("arguments "+msg+","+Arrays.toString(arguments)+" mismatch.");
		}
		StringBuffer sb=new StringBuffer();
		for(int i=0;i<a.length;i++){
			sb.append(a[i]).append(arguments[i]);
		}
		System.out.println(sb.toString());
	}

	
	public static void main(String[] args) {
		Logger logger = LoggerFactory.getLogger(LoggerImpl.class);
		logger.debug("Hello { {} I am {} }","Kevin","Jonathan");
	}
	

}
